package com.edu.hre.struts.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreLocation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.admin.OrgLocationForm;

public class OrgLocationAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(OrgLocationAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "locationInfo";
 
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		OrgLocationForm beanForm = (OrgLocationForm) form; 

		if(request.getParameter("btnEdit") != null){
			ActionErrors errors = validateForm(request, beanForm);
			if (errors != null && !errors.isEmpty()) {
				saveErrors(request, errors);
				return mapping.findForward(ACTION_NAME);
			}
			HreLocation location = new HreLocation(); 
			if(!StringUtils.isBlank(beanForm.getId())){
				location.setId(Integer.parseInt(beanForm.getId()));
			}
			location.setName(CommonUtils.normalizeString(beanForm.getName()));
			location.setProvince(CommonUtils.normalizeString(beanForm.getProvince()));
			location.setDistrict(CommonUtils.normalizeString(beanForm.getDistrict()));
			location.setAddress(CommonUtils.normalizeString(beanForm.getAddress()));
			location.setPhone(CommonUtils.normalizeString(beanForm.getPhone()));
			location.setFax(CommonUtils.normalizeString(beanForm.getFax()));
			location.setNotes(CommonUtils.normalizeString(beanForm.getNotes()));
			return mapping.findForward(ACTION_NAME);
		}
		HreLocation info =  baseService.getFirstPojo(null, -1, null, null, null, 0, HreLocation.class);
		if (info != null) {
			beanForm.setName(CommonUtils.normalizeString(info.getName()));
			beanForm.setProvince(CommonUtils.normalizeString(info.getProvince()));
			beanForm.setDistrict(CommonUtils.normalizeString(info.getDistrict()));
			beanForm.setAddress(CommonUtils.normalizeString(info.getAddress()));
			beanForm.setPhone(CommonUtils.normalizeString(info.getPhone()));
			beanForm.setFax(CommonUtils.normalizeString(info.getFax()));
			beanForm.setNotes(CommonUtils.normalizeString(info.getNotes()));
		} else {
			beanForm = new OrgLocationForm();
		} 
		return mapping.findForward(ACTION_NAME);
	} 
	
	private ActionErrors validateForm(HttpServletRequest request, OrgLocationForm beanForm) {
		ActionErrors errors = new ActionErrors();
		try { 
			MessageResources messages = getResources(request);
			if(StringUtils.isBlank(beanForm.getName())){  
				errors.add("error", new ActionMessage("errors.required", messages.getMessage(getLocale(request), "label.organization.name")));
			}
		} catch (Exception e) {
			logger.error("OrgLocationAction - validateLogin() = " + e);
		}
		return errors;
	}
}
